﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors;
using MINERP.InfoConfig.Controller;
using MINERP.MODEL.InfoConfig;
using MINERP.DAL;
using MINERP.COM;

namespace MINERP.InfoConfig
{
    public partial class FrmDelegateManager :FrmMDIChildFormTemplate
    {
        private OutProcessShop _outProcessShop = null;
        
        private int rowHandle = 1;
        private DelegateController _controller = new DelegateController();
        private ProductController _productController = new ProductController();
        private static FrmDelegateManager instance = null;
        private FrmDelegateManager()
        {
            InitializeComponent();
        }
        public static FrmDelegateManager CreateInstance
        {
            get
            {
                if (instance == null)
                {
                    instance = new FrmDelegateManager();
                }
                return instance;
            }
        }
        private void FrmDelegateManager_Load(object sender, EventArgs e)
        {
           _productController.Grid = gridControlProduct;
           _productController.View = gridViewProduct;
           _productController.GetProductData();

           //link datagrid
           _controller.Grid  = gridControlDelegate;
           _controller.View = gridViewDelegate;
           _controller.GetDate();
        }

        private void gridViewDelegate_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            rowHandle = e.FocusedRowHandle;
            _outProcessShop = (OutProcessShop)gridViewDelegate.GetFocusedRow();
            if (_outProcessShop != null)
            {
                txtOutShopName.Text = _outProcessShop.OutShopName;
                txtKeeper.Text = _outProcessShop.Keeper;
                txtTel1.Text = _outProcessShop.Tel1;
                txtTel2.Text = _outProcessShop.Tel2;
                txtCity.Text = _outProcessShop.City;
                txtAddr.Text = _outProcessShop.Addr;
                txtEmailAddress.Text = _outProcessShop.EmailAddress;
                txtCode.Text = _outProcessShop.Code;
                txtRemark.Text = _outProcessShop.Remark;
                chkIsUse.Checked = _outProcessShop.IsUse;
                checkEditAutoInGrid.Checked = _outProcessShop.AutoInGrid;
                OutProcessShop ops = _controller.GetOutProcessShopObj(_outProcessShop.OutShopId);
                _productController.GetProductData(ops);
            }
        }
       //添加
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (btnAdd.Text != "保存")
            {
                _productController.GetProductData();
                PrepareAdd(true);
                txtOutShopName.Focus();
                return;
            }
            if (CheckData())
            {
                OutProcessShop opsobj = _controller.GetOutProcessShopObj(txtOutShopName.Text);
                if (opsobj == null)
                {
                    OutProcessShop ops = new OutProcessShop();
                    ops.OutShopName = txtOutShopName.Text;
                    ops.Keeper = txtKeeper.Text;
                    ops.Tel1 = txtTel1.Text;
                    ops.Tel2 = txtTel2.Text;
                    ops.City = txtCity.Text;
                    ops.Addr = txtAddr.Text;
                    ops.EmailAddress = txtEmailAddress.Text;
                    ops.Code = txtCode.Text;
                    ops.Remark = txtRemark.Text;
                    ops.IsUse = chkIsUse.Checked;
                    ops.SHACODE = "0";
                    ops.AutoInGrid = checkEditAutoInGrid.Checked;
                    _controller.AddOutProcessShop(ops, _productController.GetOutProcessShopProduct());

                    //link datagrid
                    _controller.Grid = gridControlDelegate;
                    _controller.View = gridViewDelegate;
                    _controller.GetDate();

                    PrepareAdd(false);
                    MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("该委外商已存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
        //更新
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (btnUpdate.Text != "保存")
            {
                PrepareUpdate(true);
                txtOutShopName.Focus();
                return;
            }
            if (CheckData())
            {
                if (_outProcessShop != null)
                {
                    OutProcessShop ops = _controller.GetOutProcessShopObj(_outProcessShop.OutShopId);
                    ops.OutShopName = txtOutShopName.Text;
                    ops.Keeper = txtKeeper.Text;
                    ops.Tel1 = txtTel1.Text;
                    ops.Tel2 = txtTel2.Text;
                    ops.City = txtCity.Text;
                    ops.Addr = txtAddr.Text;
                    ops.EmailAddress = txtEmailAddress.Text;
                    ops.Code = txtCode.Text;
                    ops.Remark = txtRemark.Text;
                    ops.IsUse = chkIsUse.Checked;
                    ops.AutoInGrid = checkEditAutoInGrid.Checked;
                    _controller.UpdateOutProcessShop(ops, _productController.GetOutProcessShopProduct(),_productController.GetNoCheckShopProduct());

                    //refresh datagrid
                    _controller.GetDate();
                    PrepareUpdate(false);

                    MessageBox.Show("更新成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("请选定要更新的产品", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
            } 
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (_outProcessShop != null)
            {
                if (DialogResult.OK == MessageBox.Show("确认删除吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
                {

                    _controller.DeleteOutProcessShop(_outProcessShop);

                    //refresh datagrid
                    _controller.GetDate();
                }
            }
            else
            {
                MessageBox.Show("请选择要删除的产品！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            txtOutShopName.Enabled = false;
            txtKeeper.Enabled = false;
            txtTel1.Enabled = false;
            txtTel2.Enabled = false;
            txtCity.Enabled = false;
            txtAddr.Enabled = false;
            txtEmailAddress.Enabled = false;
            txtCode.Enabled = false;
            txtRemark.Enabled = false;
            chkIsUse.Enabled = false;

            btnAdd.Text = "添加";
            btnUpdate.Text = "更新";
            btnAdd.Enabled = true;
            btnUpdate.Enabled = true;
            btnDelete.Enabled = true;
        }

        /// <summary>
        /// 检查数据完整性
        /// </summary>
        /// <returns></returns>
        private bool CheckData()
        {
           bool flag = true;
           if (txtOutShopName.Text == string.Empty)
           {
               txtOutShopName.Focus();
               MessageBox.Show("为外商名称不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
               return false;
           }
           if (txtKeeper.Text == string.Empty)
           {
               txtKeeper.Focus();
               MessageBox.Show("联系人不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
               return false;
           }

           if (txtTel1.Text == string.Empty)
           {
               txtTel1.Focus();
               MessageBox.Show("固定电话不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
               return false;
           }

           if (txtTel2.Text == string.Empty)
           {
               txtTel2.Focus();
               MessageBox.Show("手机号不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
               return false;
           }
           if (txtCity.Text == string.Empty)
           {
               txtCity.Focus();
               MessageBox.Show("所在城市不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
               return false;
           }
           if (txtAddr.Text == string.Empty)
           {
               txtAddr.Focus();
               MessageBox.Show("地址不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
               return false;
           }
           if (txtEmailAddress.Text == string.Empty)
           {
               txtEmailAddress.Focus();
               MessageBox.Show("Email地址不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
               return false;
           }
           else
           {
               if (!txtEmailAddress.Text.Contains("@"))
               {
                   MessageBox.Show("请输入正确的Email地址", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                   return false;
               }
           }
           if (txtCode.Text == string.Empty)
           {
               txtCode.Focus();
               MessageBox.Show("为外商代号不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
               return false;
           }
           if (txtRemark.Text == string.Empty)
           {
               txtRemark.Focus();
               MessageBox.Show("备注不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
               return false;
           }
           return flag;
        }
        /// <summary>
        /// 添加前控件设置
        /// </summary>
        /// <param name="bl"></param>
        private void PrepareAdd(bool bl)
        {

           txtOutShopName.Enabled = bl;
           txtKeeper.Enabled = bl;
           txtTel1.Enabled = bl;
           txtTel2.Enabled = bl;
           txtCity.Enabled = bl;
           txtAddr.Enabled = bl;
           txtEmailAddress.Enabled = bl;
           txtCode.Enabled = bl;
           txtRemark.Enabled = bl;
           chkIsUse.Enabled = bl;

           txtOutShopName.Text = string.Empty;
           txtKeeper.Text = string.Empty;
           txtTel1.Text = string.Empty;
           txtTel2.Text = string.Empty;
           txtCity.Text = string.Empty;
           txtAddr.Text = string.Empty;
           txtEmailAddress.Text = string.Empty;
           txtCode.Text = string.Empty;
           txtRemark.Text = string.Empty;
           chkIsUse.Text = string.Empty;

           if (bl)
           {

               btnAdd.Text = "保存";
               btnUpdate.Enabled = false;
               btnDelete.Enabled = false;
           }
           else
           {
               btnAdd.Text = "添加";
               btnUpdate.Enabled = true;
               btnDelete.Enabled = true;
           }
        }

        /// <summary>
        /// 更新前控件设置
        /// </summary>
        /// <param name="bl"></param>
        private void PrepareUpdate(bool bl)
        {
            txtOutShopName.Enabled = bl;
            txtKeeper.Enabled = bl;
            txtTel1.Enabled = bl;
            txtTel2.Enabled = bl;
            txtCity.Enabled = bl;
            txtAddr.Enabled = bl;
            txtEmailAddress.Enabled = bl;
            txtCode.Enabled = bl;
            txtRemark.Enabled = bl;
            chkIsUse.Enabled = bl;

            btnAdd.Enabled = bl;
            btnDelete.Enabled = bl;

           if (bl)
           {

               btnUpdate.Text = "保存";
               btnAdd.Enabled = false;
               btnDelete.Enabled = false;
           }
           else
           {
               btnUpdate.Text = "更新";
               btnAdd.Enabled = true;
               btnDelete.Enabled = true;
           }
        }

        private void FrmDelegateManager_FormClosed(object sender, FormClosedEventArgs e)
        {
            instance = null;
        }

        private void txtOutShopName_Leave(object sender, EventArgs e)
        {
            //OutProcessShop ops = _controller.GetOutProcessShopObj(
        }

        private void gridViewProduct_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (e.Column.FieldName == "IsProductDefaultShop")
            {
                if ((bool)e.Value)
                {
                    OutProcessShop outShop =null;
                    ProductEntity productEntity = (ProductEntity)gridViewProduct.GetFocusedRow();
                    if (_controller.ProductHasDefaultOutShop(productEntity.ProuctId, out outShop))
                    {
                        if (DialogResult.OK == MessageBox.Show("该产品已经制定默认委外商为" + outShop.OutShopName + "改变默认为外商点‘确定’,否则点‘取消’", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
                        {
                            _controller.ChangeDefaultOutShop(productEntity.ProuctId, outShop.OutShopId);
                            productEntity.IsProductDefaultShop = true;
                        }
                        else
                        {
                            productEntity.IsProductDefaultShop = false;
                        }
                    }
                    else
                    {
                        productEntity.IsProductDefaultShop = true;
                    }
                    gridViewProduct.RefreshData();
                }
            }
        }

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < gridViewProduct.DataRowCount; i++)
            {
                ProductEntity productEntity = (ProductEntity)gridViewProduct.GetRow(i);
                productEntity.IsChecked = true;
            }
            gridViewProduct.RefreshData();
        }

        private void btnActiveCode_Click(object sender, EventArgs e)
        {
            try
            {
                OutProcessShop op = (OutProcessShop)gridViewDelegate.GetFocusedRow();
                _controller.ProductActiveCode(op.OutShopId);
                _controller.GetDate();
                MessageBox.Show("生成激活码成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

    }
}
